package com.yxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.common.PageResult;
import com.yxy.constant.MessageConstant;
import com.yxy.constant.StatusConstant;
import com.yxy.context.BaseContext;
import com.yxy.exception.SightsAlreadyExistsException;
import com.yxy.mapper.CollectMapper;
import com.yxy.mapper.SightsMapper;
import com.yxy.pojo.dto.SightsDTO;
import com.yxy.pojo.dto.SightsPageQueryDTO;
import com.yxy.pojo.entity.SightsCollect;
import com.yxy.pojo.entity.Sights;
import com.yxy.pojo.vo.SightsPageQueryVO;
import com.yxy.pojo.vo.SightsVo;
import com.yxy.service.SightsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SightsServiceImpl implements SightsService {

    @Autowired
    private SightsMapper sightsMapper;
    @Autowired
    private CollectMapper collectMapper;

    /**
     * 分页查询
     *
     * @param sightsPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SightsPageQueryDTO sightsPageQueryDTO) {
        PageHelper.startPage(sightsPageQueryDTO.getPage(), sightsPageQueryDTO.getPageSize());
        Page<SightsPageQueryVO> page = sightsMapper.pageQuery(sightsPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增
     *
     * @param sightsDTO
     */
    @Override
    public void add(SightsDTO sightsDTO) {
        // 根据景点名称查询景点是否已存在
        Sights sights = sightsMapper.selectByName(sightsDTO.getName());
        if (sights != null) {
            throw new SightsAlreadyExistsException(MessageConstant.SIGHTS_ALREADY_EXISTS);
        }

        Sights build = Sights.builder()
                .name(sightsDTO.getName())
                .price(sightsDTO.getPrice())
                .address(sightsDTO.getAddress())
                .introduce(sightsDTO.getIntroduce())
                .cover(sightsDTO.getCover())
                .status(sightsDTO.getStatus())
                .createTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .build();
        sightsMapper.insert(build);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(int id, int status) {
        sightsMapper.updateStatus(id, status);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        sightsMapper.deleteByIds(ids);
    }

    /**
     * 修改
     *
     * @param sightsDTO
     */
    @Override
    public void update(SightsDTO sightsDTO) {
        Sights sights = Sights.builder()
                .id(sightsDTO.getId())
                .name(sightsDTO.getName())
                .price(sightsDTO.getPrice())
                .address(sightsDTO.getAddress())
                .introduce(sightsDTO.getIntroduce())
                .cover(sightsDTO.getCover())
                .status(sightsDTO.getStatus())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        sightsMapper.update(sights);
    }

    /**
     * 分页查询（不包括下架状态）
     *
     * @param sightsPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQueryEnable(SightsPageQueryDTO sightsPageQueryDTO) {
        PageHelper.startPage(sightsPageQueryDTO.getPage(), sightsPageQueryDTO.getPageSize());
        Page<SightsPageQueryVO> page = sightsMapper.pageQueryEnable(sightsPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加浏览量
     *
     * @param id
     */
    @Override
    public void addViews(Integer id) {
        // 查询当前景点的浏览量
        Integer views = sightsMapper.selectViews(id);
        if (views == null) {
            views = 0;
        }
        // 浏览量加一
        sightsMapper.addViews(id, views + 1);
    }

    /**
     * 获取景点详情
     *
     * @param id
     * @return
     */
    @Override
    public SightsVo getSightsDetail(Integer id) {
        // 查询景点信息
        Sights sights = sightsMapper.selectById(id);
        SightsVo vo = new SightsVo();
        BeanUtils.copyProperties(sights, vo);
        // 查询用户是否收藏
        SightsCollect sightsCollect = collectMapper.selectByUserIdAndSightsId(BaseContext.getCurrentId(), id);
        // 如果查询为空，表明用户第一次进该景点详情
        if (sightsCollect == null) {
            // 则进行添加操作，值为0（未收藏）
            collectMapper.addSightsCollect(SightsCollect
                    .builder()
                    .userId(BaseContext.getCurrentId())
                    .sightsId(id)
                    .status(StatusConstant.DEFAULT_COLLECT)
                    .build());
        }
        // 把用户的收藏状态添加到返回值中
        vo.setStatus(sightsCollect == null ? StatusConstant.DEFAULT_COLLECT : sightsCollect.getStatus());
        return vo;
    }
}
